﻿using FineUI;
using System;
using System.Linq;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Common;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.PartyCare
{
    public partial class JobHelpingEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        public bool IsTask
        {
            get { return Convert.ToString(Request.QueryString["Task"]) == "True"; }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = article.Title;
                ppBranch.Value = article.BranchID;
                ppBranch.Text = ControlBusiness.GetBranchPickerText(article.BranchID);
                this.dpBeginDate.Text = Convert.ToDateTime(article.ActivityBeginTime).ToString("yyyy-MM-dd");
                this.dpEndDate.Text = Convert.ToDateTime(article.ActivityEndTime).ToString("yyyy-MM-dd");
                this.editor.Text = article.Content;
                this.txtCreateBy.Text = Convert.ToString(PartyOrg.GetUserMessageByloginID(article.CreateBy).Rows[0]["UserName"]);
            }
            else
            {
                hdArticleID.Text = StringHelper.NewID;
                txtCreateBy.Text = this.CurrentUser.UserName;
                ppBranch.Value = this.BranchID;
                ppBranch.Text = ControlBusiness.GetBranchPickerText(ppBranch.Value);
            }

            Grid1.DataSource = PartyArticle.GetFileData(id);
            Grid1.DataBind();
        }
        #endregion

        #region Events

        private void SaveData(bool isSubmit)
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;

            if (id != "")
            {
                //更新
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                article.Title = txtTitle.Text.Trim();
                article.BranchID = ppBranch.Value;
                article.ModifyTime = DateTime.Now;
                if (isSubmit)
                {
                    if (PartyOrg.IsBranchAdmin(CurrentUser.LoginID, CurrentUser.BranchID))
                        article.Status = 4;
                    else
                    {
                        article.Status = 1;
                        //添加组织待办
                        PartyTaskHelper.AddTaskToBranch(article.ID, "发布党内招聘", SysEnum.TaskCategory.PartyRecruit,
                                article.CreateBy, new string[] { CurrentUser.BranchID },
                                "../../../../App/PersonalCenter/Module/PartyCare/JobHelpingEdit.aspx?id=" +
                                article.ID + "&Task=True");
                    }
                    if (IsTask)
                    {
                        ///代办变已办
                        PartyTaskHelper.TaskDone(article.ID, this.CurrentUser.LoginID);
                    }
                }
                else
                {
                    article.Status = 1;
                }
                //article.Status = isSubmit ? 4 : 1;
                article.Content = editor.Text;
                article.ModifyBy = CurrentUser.LoginID;
                article.ActivityBeginTime = Convert.ToDateTime(this.dpBeginDate.Text);
                article.ActivityEndTime = Convert.ToDateTime(this.dpEndDate.Text);

                DB.SaveChanges();

            }
            else
            {
                PartyArticle article = new PartyArticle();
                article.ID = hdArticleID.Text;
                article.Module = "partylove";
                article.Category = "PartyRecruit";
                article.Title = txtTitle.Text.Trim();
                article.BranchID = ppBranch.Value;
                if (isSubmit)
                {
                    if (PartyOrg.IsBranchAdmin(CurrentUser.LoginID, CurrentUser.BranchID))
                        article.Status = 4;
                }
                else
                {
                    article.Status = 1;
                    //添加组织待办
                    PartyTaskHelper.AddTaskToBranch(article.ID, "发布党内招聘", SysEnum.TaskCategory.PartyRepresentative,
                            article.CreateBy, new string[] { CurrentUser.BranchID },
                            "../../../../App/PersonalCenter/Module/PartyCare/JobHelpingEdit.aspx?id=" +
                            article.ID + "&Task=True");
                }
                //article.Status = isSubmit ? 4 : 1;
                article.Content = editor.Text;
                article.CreateTime = DateTime.Now;
                article.ModifyTime = DateTime.Now;
                article.CreateBy = CurrentUser.LoginID;
                article.ModifyBy = CurrentUser.LoginID;

                DB.PartyArticles.Add(article);

                DB.SaveChanges();


            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(false);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SaveData(true);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string id = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                var del = DB.PartyArticlesFiles.FirstOrDefault(t => t.ID == id);
                DB.PartyArticlesFiles.Remove(del);
                DB.SaveChanges();

                Grid1.DataSource = PartyArticle.GetFileData(hdArticleID.Text);
                Grid1.DataBind();
            }
        }

        protected void fileImage_FileSelected(object sender, EventArgs e)
        {
            fileImage.Reset();
            UpFileResult upfileResult = UpFileBusiness.Upload(fileImage, CurrentUser.LoginID);
            if (upfileResult.IsSuccess)
            {
                PartyArticlesFile articleFile = new PartyArticlesFile();
                articleFile.ID = StringHelper.NewID;
                articleFile.ArticleID = hdArticleID.Text;
                articleFile.FileID = upfileResult.FileID;
                articleFile.CreateTime = DateTime.Now;
                articleFile.ModifyTime = DateTime.Now;
                articleFile.CreateBy = CurrentUser.LoginID;
                articleFile.ModifyBy = CurrentUser.LoginID;

                DB.PartyArticlesFiles.Add(articleFile);

                DB.SaveChanges();

                Grid1.DataSource = PartyArticle.GetFileData(hdArticleID.Text);
                Grid1.DataBind();
            }
            else
            {
                Alert.Show("上传失败！" + upfileResult.Message, String.Empty);
            }
        }

        #endregion
    }
}